package fr.afcepf.iziticket.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import fr.afcepf.iziticket.entities.Commande;
import fr.afcepf.iziticket.services.HistoriqueService;

@ManagedBean(name="mbHistorique")
@SessionScoped
public class HistoriqueManagedBean {

	private HistoriqueService service = HistoriqueService.getService();
	
	@ManagedProperty(value="#{mbLogin}")
	private LoginManagedBean mbLogin;
	
	private List<Commande> commandes = new ArrayList<Commande>();
	
	
	public List<Commande> getCommandes() {
		return commandes;
	}
	public void setCommandes(List<Commande> commandes) {
		this.commandes = commandes;
	}
	public LoginManagedBean getMbLogin() {
		return mbLogin;
	}
	public void setMbLogin(LoginManagedBean mbLogin) {
		this.mbLogin = mbLogin;
	}
	
	public void getCommandesFromUtilisateur(ActionEvent ae){

		int idUtil = Integer.parseInt(ae.getComponent().getAttributes().get("idUtilisateur").toString());
		
		List<Commande> commTemp = service.getCommandesFromUtilisateur(idUtil);
		
		this.commandes.clear();
		
		for(int i = commTemp.size()-1; i>=0;i--){
			this.commandes.add(commTemp.get(i));
		}
		
		FacesContext facesContext = FacesContext.getCurrentInstance();
		NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
	    myNav.handleNavigation(facesContext, null, "/historique.xhtml");
		
	}
	
}
